/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */



var xm = window.xm || {};
var cordova = window.cordova || {};
var buffer;
var _cubes;
var appDir;
var medias =[];

function reloadTable() {

    console.log('xxxxx')
}

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        console.log('deviceReady');
        appDir = cordova.file.applicationDirectory;

        medias = [
            // {path : 'file:///android_asset/www/img/1.wlp'},
            // {text : 'B', backgroundColor: 'white', textColor: 'black'},
            // {text : 'supersuper man', backgroundColor: 'white', textColor: 'black'},
            // {text : '1', backgroundColor: 'white', textColor: 'black'},
            // {text : '2', backgroundColor: 'white', textColor: 'black'},
            // {text : '3', backgroundColor: 'white', textColor: 'black'},
            // {text : '4', backgroundColor: 'white', textColor: 'black'},
            // {text : '5', backgroundColor: 'white', textColor: 'black'},
            // {text : '中国人', backgroundColor: 'white', textColor: 'black'},
            // {text : 'Hel', backgroundColor: 'white', textColor: 'black'},
            // {text : '123', backgroundColor: 'white', textColor: 'black'},
            // {text : 'monkey', backgroundColor: 'white', textColor: 'black'},
            // {text : '3c', backgroundColor: 'white', textColor: 'black'},
            // {text : '4d', backgroundColor: 'white', textColor: 'black'},
            // {text : '5e', backgroundColor: 'white', textColor: 'black'},
            // {text : 'cat', backgroundColor: 'white', textColor: 'black'},
            // {text : 'dog', backgroundColor: 'white', textColor: 'black'},
            {path : appDir + 'www/assets/img/gorilla.wlp'},
            // {text : 'A', backgroundColor: 'white', textColor: 'black'},
            {text : 'B', backgroundColor: 'white', textColor: 'black'}

        ];

        alert('ready OK');
        alert(appDir);

        // xm.requestCubes(2, function (cubes) {
        //     _cubes = cubes;
        //     alert('request ok')
        // },null);
        //
        // xm.sendCubesMedias(_cubes,medias,false,function () {
        //     console.log('send succ')
        // },function () {
        //     console.log('send err')
        // });
        //
        // xm.registerOnShake(function (cube, num) {
        //     alert(cube.media.text);
        // });

        function test0() {
            xm.initialize(function () {
                alert('initial success')
            })
        }
        function test1() {
            xm.requestCubes(2, function (cubes) {
                _cubes = cubes;
                alert('request ok')
            },null)
        }
        function test2() {
            xm.sendCubesMedias(_cubes,medias,false,function () {
                console.log('send succ')
            },function () {
                console.log('send err')
            });
        }
        function test3() {
            alert('test3');
            console.log('test3')
        }

        document.getElementById('test0').addEventListener('click',function () {
            test0();
            // alert('test111')
        });
        document.getElementById('test1').addEventListener('click',function () {
            test1();
            // alert('test111')

        });
        document.getElementById('test2').addEventListener('click',function () {
            test2();
            // alert('test111')

        });
        document.getElementById('test3').addEventListener('click',function () {
            test3();
            // alert('test111')

        });

        xm.registerOnShake(function (cube) {
            var val = cube.media.text;
            document.getElementById('out').value = val;
        })

    },
};

app.initialize();